package com.lcxw.hxzbapi.dao.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;
import com.lcxw.hxzbapi.entity.pojo.GiftSend;

@Component
@Mapper
public interface GiftSendMapper {

    //插入一条
    @Insert("INSERT INTO ywpd_giftsend (roomId,userId,fromUserId,source,giftId,giftNum,needCoin,addTime) VALUES (#{roomId},#{userId},#{fromUserId},#{source},#{giftId},#{giftNum},#{needCoin},#{addTime})")
    int insertGiftEarn(GiftSend giftSend) throws Exception;

    //通过送礼人的 userId 查询 即 fromUserId
    @Select("SELECT * FROM ywpd_giftsend WHERE fromUserId=#{fromUserId}")
    List<GiftSend> selectByFromUserId(long fromUserId) throws Exception;

    //通过送礼人的 userId 查询 即 fromUserId,支持分页
    @Select("SELECT * FROM ywpd_giftsend WHERE fromUserId=#{fromUserId} LIMIT #{pageNum},#{pageSize}")
    List<GiftSend> selectByFromUserIdWithPage(@Param("fromUserId") long fromUserId,
        @Param("pageNum") int pageNum, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM ywpd_giftsend WHERE fromUserId=#{fromUserId}")
    int selectCountByfromUserId(long fromUserId);

    //按  userId 即 接受礼物的用户id查询
    @Select("SELECT * FROM ywpd_giftsend WHERE userId=#{userId}")
    List<GiftSend> selectByUserId(long userId);

    //按  userId 即 接受礼物的用户id查询,支持分页
    @Select("SELECT a.* FROM ywpd_giftsend a left join ywpd_gift b on a.giftId=b.id WHERE a.userId=#{userId} and b.tixian=1 order by id desc LIMIT #{pageNum},#{pageSize}")
    List<GiftSend> selectByUserIdWithPage(@Param("userId") long userId,
        @Param("pageNum") int pageNum, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM ywpd_giftsend a left join ywpd_gift b on a.giftId=b.id WHERE a.userId=#{userId} and b.tixian=1")
    int selectCountByUserId(long userId);


    //按照 userId 和 fromUserId查询
    @Select("SELECT * FROM ywpd_giftsend WHERE userId=#{userId} AND fromUserId=#{fromUserId}")
    List<GiftSend> selectByUserIdIdAndFromUserId(@Param("userId") long userId,
        @Param("fromUserId") long fromUserId) throws Exception;

    //按  roomId 查询
    @Select("SELECT * FROM ywpd_giftsend WHERE roomId=#{roomId}")
    List<GiftSend> selectByRoomId(long roomId);

    //按  roomId 查询 addTime 以后的
    @Select("SELECT * FROM ywpd_giftsend WHERE roomId=#{roomId} AND addTime >= #{addTime}")
    List<GiftSend> selectCurrentDayByRoomId(@Param("roomId") long roomId,
        @Param("addTime") String addTime);

    //更新一条记录
    @Update("UPDATE ywpd_giftsend SET userId=#{userId}, source=#{source},giftId=#{giftId}, giftNum=#{giftNum}, needCoin=#{needCoin} WHERE roomId=#{roomId} AND giftId=#{giftId}")
    int updateByRoomId(GiftSend gift) throws Exception;

    //查询所有
    @Select("SELECT * FROM ywpd_giftsend")
    List<GiftSend> selectAll() throws Exception;

    //按时间查询，查询传入时间以后的所有记录
    @Select("SELECT * FROM ywpd_giftsend WHERE addTime >= #{time}")
    List<GiftSend> selectAfterTime(String time) throws Exception;

}
